package com.JFapp.controller;


import com.JFapp.domain.dto.MenuDto;
import com.JFapp.domain.entity.Menu;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.eumns.AppHttpCodeEnum;
import com.JFapp.exception.SystemException;
import com.JFapp.service.MenuService;
import com.JFapp.utils.BeanCopyUtils;
import com.JFapp.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/menu")
@Api("菜单管理")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("查询菜单列表")
    @GetMapping("/list")
    public ResponseResult getMenuList(String status,String menuName){
        return menuService.getMenuList(status,menuName);}


    @ApiOperation("更改菜单状态")
    @PostMapping("/menuId")
    public ResponseResult changeMenuStatus(@Param("menuId") Long menuId,@Param("status") String status){
        if (SecurityUtils.isAdmin()) {
            return menuService.changeMenuStatus(menuId,status);
        } else {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

    }
    @ApiOperation("查询菜单详细")
    @GetMapping("/{menuId}")
    public ResponseResult getMenu(@PathVariable Long menuId){
        return menuService.getMenuById(menuId);
    }

    @ApiOperation("查询菜单树结构")
    @GetMapping("/treeselect")
    public ResponseResult getMenuTreeList(){
        return menuService.getMenuTreeList();
    }


    @ApiOperation("根据id查询菜单树结构")
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public ResponseResult getRoleMenuTreeById(@PathVariable("roleId") Long id){
        return menuService.getRoleMenuTreeById(id);
    }

    @ApiOperation("新增菜单")
    @PostMapping
    public  ResponseResult addMenu(@RequestBody MenuDto menuDto){
        Menu menu =  BeanCopyUtils.copyBean(menuDto, Menu.class);
        return  menuService.addMenu(menu);
    }
    @ApiOperation("删除菜单")
    @DeleteMapping("/{menuId}")
    public ResponseResult deleteMenu(@PathVariable("menuId") Long menuId){
        return menuService.deleteMenu(menuId);
    }

    @ApiOperation("更新菜单")
    @PutMapping
    public  ResponseResult updateMenu(@RequestBody MenuDto menuDto){
        Menu menu = BeanCopyUtils.copyBean(menuDto, Menu.class);
        return menuService.updateMenu(menu);
    }
}
